package com.example.patentback.service.impl;

import cn.hutool.json.JSONUtil;
import com.example.patentback.dao.SysMenuMapper;
import com.example.patentback.dao.SysUserMapper;
import com.example.patentback.entity.SysMenu;
import com.example.patentback.entity.SysMenuDto;
import com.example.patentback.entity.SysUser;
import com.example.patentback.service.SysMenuService;
import com.example.patentback.service.SysUserService;
import com.example.patentback.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserService.getByUsername(username);

        List<Long> menuIds = sysUserMapper.getNavMenuIds(sysUser.getId());
        Example example = new Example(SysMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", menuIds);
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(example);

        // 转树状结构
        List<SysMenu> menuTree = buildTreeMenu(sysMenus);

        // 实体转DTO
        return convert(menuTree);
    }

    @Override
    public List<SysMenu> getAllMenus() {
        // 获取所有菜单信息
        Example example = new Example(SysMenu.class);
        example.orderBy("ordernum");
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(example);

        // 转成树状结构
        return buildTreeMenu(sysMenus);
    }

    private List<SysMenuDto> convert(List<SysMenu> menuTree) {
        List<SysMenuDto> menuDtos = new ArrayList<>();

        menuTree.forEach(m -> {
            SysMenuDto dto = new SysMenuDto();

            dto.setId(m.getId());
            dto.setIcon(m.getIcon());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setComponent(m.getComponent());
            dto.setPath(m.getPath());

            if (m.getChildren().size() > 0) {

                // 子节点调用当前方法进行再次转换
                dto.setChildren(convert(m.getChildren()));
            }

            menuDtos.add(dto);
        });

        return menuDtos;
    }

    private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {

        List<SysMenu> finalMenus = new ArrayList<>();

        // 先各自寻找到各自的孩子
        for (SysMenu menu : menus) {

            for (SysMenu e : menus) {
                if (menu.getId().equals(e.getParentId())) {
                    menu.getChildren().add(e);
                }
            }

            // 提取出父节点
            if (menu.getParentId() == 0L) {
                finalMenus.add(menu);
            }
        }
        //System.out.println(JSONUtil.toJsonStr(finalMenus));
        return finalMenus;
    }

    //根据id获取某一个菜单详情
    @Override
    public SysMenu getMenuById(Long id) {
        Example example = new Example(SysMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(example);
        return sysMenus.get(0);
    }

    //新建菜单
    @Override
    public int saveMenu(SysMenu sysMenu) {
        sysMenu.setCreated(new Date());
        return sysMenuMapper.insertUseGeneratedKeys(sysMenu);
    }

    @Override
    public int updateMenu(SysMenu sysMenu) {
        sysMenu.setUpdated(new Date());
        return sysMenuMapper.updateByPrimaryKey(sysMenu);
    }

    @Override
    public int deleteMenu(Long id) {
        Example example = new Example(SysMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", id);
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(example);
        if (sysMenus.size() > 0) {
            return 0;
        }
        return sysMenuMapper.deleteByPrimaryKey(id);
    }
}
